package com.uton.rfidcontroller.view.activity.scan;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.GlobalConstants;
import com.uton.rfidcontroller.common.intent.IntentBuilder;
import com.uton.rfidcontroller.databinding.ActivityScanDetailBinding;
import com.uton.rfidcontroller.view.activity.CarDetailActivity;
import com.uton.rfidcontroller.view.base.BaseActivity;
import com.uton.rfidcontroller.viewmodel.activity_model.ScanDetailActivityViewModel;

import butterknife.BindView;
import io.reactivex.ObservableTransformer;

/**
 * Created by 王家宝 on 2017/9/25.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public class ScanDetailActivity extends BaseActivity<ActivityScanDetailBinding, ScanDetailActivityViewModel> {
    @BindView(R.id.spinner)
    Spinner mSpinner;

    private int currentPos;

    @Override
    protected int setLayout() {
        return R.layout.activity_scan_detail;
    }

    @Override
    protected void inject() {
        getBinding().setModel(getViewModel());
    }

    @Override
    protected String setTitle() {
        return getString(R.string.ui_device);
    }

    @Override
    protected void doSomething() {
        if (GlobalConstants.LOCKED.equals(getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM2))) {
            //数据被锁定，不能修改
            getViewModel().locked.set(true);
        } else {
            //数据未被锁定，可以修改
            getViewModel().locked.set(false);
        }
        //给卡片ID赋值
        getViewModel().cardId.set(getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM1));

        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                currentPos = i;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

        switch (getIntent().getIntExtra(GlobalConstants.EXTRA_INT_PARAM2, -1)) {
            case GlobalConstants.SCAN_ERROR:
                getViewModel().isError.set(true);
                getViewModel().errMsg.set(getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM3));
                break;
            default:
                break;
        }
    }

    @Override
    protected ScanDetailActivityViewModel setViewModel() {
        return new ScanDetailActivityViewModel(
                getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM4),
                getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM5)
        ) {
            @Override
            public ObservableTransformer setObservable() {
                return compose(bindToLifecycle());
            }

            @Override
            public void showCarDetail() {
                startActivity(new IntentBuilder().from(ScanDetailActivity.this, CarDetailActivity.class).
                        setStringParam(GlobalConstants.EXTRA_STRING_PARAM1, getIntent().
                                getStringExtra(GlobalConstants.EXTRA_STRING_PARAM4)).
                        getIntent());
            }

            @Override
            public void commitRet() {
                setResult(GlobalConstants.RESULT_SAVE_RET, new IntentBuilder()
                        .from(ScanDetailActivity.this, ScanActivity.class)
                        .setStringParam(GlobalConstants.EXTRA_STRING_PARAM1,
                                getResources().getStringArray(R.array.error_type)[currentPos])
                        .setIntParam(GlobalConstants.EXTRA_INT_PARAM1, getIntent().getIntExtra(GlobalConstants.EXTRA_INT_PARAM1, -1))
                        .getIntent());
                finish();
            }
        };
    }


}
